package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.base.SubAreaService;
import cn.itcast.bos.utils.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//分区的action
@Namespace("/")
@ParentPackage("json-default")
@Scope("protoType")
@Controller
public class SubAreaAction extends BaseAction<SubArea> {
    private File file;

    public void setFile(File file) {
        this.file = file;
    }

    @Autowired
    private SubAreaService subAreaService;
    //导入文件的方法
    @Action(value = "subarea_import")
    public void importSubArea(){
        //定义一个集合,存储SubArea
        List<SubArea> subAreas = new ArrayList<>();
        //解析xls文件
        try {
            XSSFWorkbook hssf = new XSSFWorkbook(new FileInputStream(file));
            XSSFSheet sheet = hssf.getSheetAt(0);
            for(Row row :sheet){
                //跳过第一行
                if(row.getRowNum()==0){
                   continue;
                }
                //跳过空行
                if(row.getCell(0)==null || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
                    continue;
                }
                model.setId(row.getCell(0).getStringCellValue());
               /* Area area = new Area();
                area.setProvince(row.getCell(1).getStringCellValue());
                area.setCity(row.getCell(2).getStringCellValue());
                area.setDistrict(row.getCell(3).getStringCellValue());*/
               // model.setArea(area);
                model.setKeyWords(row.getCell(4).getStringCellValue());
                model.setStartNum(row.getCell(5).getStringCellValue());
                model.setEndNum(row.getCell(6).getStringCellValue());
                model.setSingle(row.getCell(7).getStringCellValue());
                model.setAssistKeyWords(row.getCell(8).getStringCellValue());
                //调用服务层
                subAreaService.save(model);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
